<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <meta name="description" content="任务进度可视化，python进度条模块Tqdm
1Tqdm是一个易用性强,拓展性高的python进度条库，可以在Python长循环中添加一个进度提示信息，我们只需要封装任意的迭代器即可。

安装1pip install tqdm

自动控制更新速度代码如下：
1234import timefrom " />
  

  
  
  
  
  
  
  <title>进度条 | 南辞的技术博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="任务进度可视化，python进度条模块Tqdm 1Tqdm是一个易用性强,拓展性高的python进度条库，可以在Python长循环中添加一个进度提示信息，我们只需要封装任意的迭代器即可。  安装1pip install tqdm  自动控制更新速度代码如下： 1234import timefrom tqdm import *for i in trange(100):    time.sleep(0">
<meta property="og:type" content="article">
<meta property="og:title" content="进度条">
<meta property="og:url" content="https://yanlidoushikeke.gitee.io/myhexo/2021/03/24/%E8%BF%9B%E5%BA%A6%E6%9D%A1/index.html">
<meta property="og:site_name" content="南辞的技术博客">
<meta property="og:description" content="任务进度可视化，python进度条模块Tqdm 1Tqdm是一个易用性强,拓展性高的python进度条库，可以在Python长循环中添加一个进度提示信息，我们只需要封装任意的迭代器即可。  安装1pip install tqdm  自动控制更新速度代码如下： 1234import timefrom tqdm import *for i in trange(100):    time.sleep(0">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-03-24T06:23:45.000Z">
<meta property="article:modified_time" content="2021-03-24T08:48:54.926Z">
<meta property="article:author" content="南辞">
<meta name="twitter:card" content="summary">
  
  
    <link rel="icon" href="/tefuir/css/images/favicon.ico">
  
  
<link rel="stylesheet" href="/tefuir/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/tefuir/atom.xml" title="南辞的技术博客" type="application/atom+xml">
</head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/tefuir/" title="南辞的技术博客" rel="home">南辞的技术博客</a>
      </h1>
      
        <h2 class="site-description hitokoto"></h2>
        <script type="text/javascript" src="https://v1.hitokoto.cn/?encode=js"></script>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/">Home</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/archives">Archives</a></li>
                
                </ul>
            </div>
    </nav>
</header>

      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-进度条" class="post-进度条 post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      进度条
    </h1>
  

        
        <div class="comments-link">
            
            <a href="javascript:void(0);" data-url="https://yanlidoushikeke.gitee.io/myhexo/2021/03/24/%E8%BF%9B%E5%BA%A6%E6%9D%A1/" data-id="ckmn4ijy10000k0u07iud1vne" class="leave-reply bdsharebuttonbox" data-cmd="more">Share</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <p>任务进度可视化，python进度条模块Tqdm</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Tqdm是一个易用性强,拓展性高的python进度条库，可以在Python长循环中添加一个进度提示信息，我们只需要封装任意的迭代器即可。</span><br></pre></td></tr></table></figure>

<h4 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install tqdm</span><br></pre></td></tr></table></figure>

<h4 id="自动控制更新速度"><a href="#自动控制更新速度" class="headerlink" title="自动控制更新速度"></a>自动控制更新速度</h4><p>代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line">from tqdm import *</span><br><span class="line">for i in trange(100):</span><br><span class="line">    time.sleep(0.1)</span><br></pre></td></tr></table></figure>

<h4 id="使用方法trange"><a href="#使用方法trange" class="headerlink" title="使用方法trange"></a>使用方法trange</h4><p>trange(i)是Sqdm(range(i))的简单写法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line">from tqdm import *</span><br><span class="line">for i in trange(100):</span><br><span class="line">    time.sleep(0.1)</span><br></pre></td></tr></table></figure>

<h4 id="为进度条设置描述"><a href="#为进度条设置描述" class="headerlink" title="为进度条设置描述"></a>为进度条设置描述</h4><p>在for循环外部初始化tqdm,可以作为信息打印在进度条中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line">from tqdm import *</span><br><span class="line"></span><br><span class="line">pdar &#x3D; tqdm([&#39;download&#39;,&#39;install&#39;,&#39;debug&#39;,&#39;run&#39;])</span><br><span class="line">for char in pdar:</span><br><span class="line">    with open(&#39;a.txt&#39;,&#39;w&#39;) as w:</span><br><span class="line">        w.write(char)</span><br><span class="line"></span><br><span class="line">    pdar.set_description(&#39;Processing %s&#39;% char )</span><br><span class="line">    time.sleep(1)</span><br></pre></td></tr></table></figure>



<p>另外，在使用tqdm显示进度条的时，若代码中存在print可能会使得输出杂乱，此时可以将print语句替换为tqdm.write，代码如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line">from tqdm import *</span><br><span class="line">for i in trange(10):</span><br><span class="line">    time.sleep(0.1)</span><br><span class="line">    if not (i % 2):</span><br><span class="line">        tqdm.write(&#39;Done task &#123;0&#125;&#39;.format(i))</span><br><span class="line"></span><br></pre></td></tr></table></figure>




      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/tefuir/2021/03/24/%E8%BF%9B%E5%BA%A6%E6%9D%A1/">
    <time datetime="2021-03-24T06:23:45.000Z" class="entry-date">
        2021-03-24
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/tefuir/2021/03/25/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="prev"><span class="meta-nav">←</span> 数据库</a></span>
    
    
        <span class="nav-next"><a href="/tefuir/2021/03/23/%E6%B6%88%E6%81%AF%E6%92%A4%E5%9B%9E/" rel="next">消息撤回 <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->







</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside id="search" class="widget widget_search"><form role="search" method="get" accept-charset="utf-8" id="searchform" class="searchform" action="//google.com/search">
    <div>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="搜索" />
    </div>
</form></aside>
  
    
  
    
  
    
  <aside class="widget">
    <h3 class="widget-title">Recents</h3>
    <div class="widget-content">
      <ul>
        
          <li>
            <a href="/tefuir/2021/04/26/python%E5%9F%BA%E7%A1%80%E7%90%86%E8%AE%BA%E5%AE%9D%E5%85%B8/">python基础理论宝典</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/python3-Flask%E7%BB%93%E5%90%88Socket-io%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E5%9C%A8%E7%BA%BF%E5%AE%A2%E6%9C%8D%E7%B3%BB%E7%BB%9F/">python3+Flask结合Socket.io简单实现在线客服系统</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%88%A9%E7%94%A8docker%E9%83%A8%E7%BD%B2gunicorn-Flask%E6%89%93%E9%80%A0%E7%8B%AC%E7%AB%8B%E9%95%9C%E5%83%8F/">win10系统下利用docker部署gunicorn+Flask打造独立镜像</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/">win10系统下基于docker配置elasticsearch配合python3进行全文检索</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%97%AE%E9%A2%98/">服务器问题</a>
          </li>
        
      </ul>
    </div>
  </aside>

  
    
  
    
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2021 南辞
    All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/js/share.js'];</script>

<script src="/js/jquery-3.3.1.min.js"></script>


  
<link rel="stylesheet" href="/tefuir/fancybox/jquery.fancybox.css">

  
<script src="/tefuir/fancybox/jquery.fancybox.pack.js"></script>




<script src="/tefuir/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>